<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\BaseTimelines as BaseTimelinesModel;
use app\model\RedisModel;
use think\App;

class BaseTimelines extends Base {

    protected $middleware = [Auth::class];
    private $baseTimelinesModel;

    public $cacheKey="base:timelines:id:";

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->baseTimelinesModel = new  BaseTimelinesModel();

        $this->isLogin();

    }



    /*
     * 获取店铺列表
     * */
    public function getList(){

        $list = $this->baseTimelinesModel->getList();

        $total = $this->baseTimelinesModel->getTotal();

        return $this->succData($total,$list);
    }



    /*
    * 修改
    * */
    public function edit()
    {
        if(!isset($this->req['timelines_id']) || empty($this->req['timelines_id'])){
            return $this->fail("请选择要修改的时效");
        }

        if(!isset($this->req['timelines_name']) || empty($this->req['timelines_name'])){
            return $this->fail("名称不能为空");
        }

        if(!isset($this->req['timelines_time']) || empty($this->req['timelines_time'])){
            return $this->fail("请填写发货时效");
        }


        $res = $this->baseTimelinesModel->updateOne($this->req['timelines_id'],[
                'timelines_name' => $this->req['timelines_name'],
                'timelines_time' => $this->req['timelines_time'],
            ]
        );
        if($res){
            $this->delCache($this->req['timelines_id']);
            return $this->succ();
        }else{
            return $this->fail();
        }
    }


    public function delCache($id)
    {
        $redisModel = new RedisModel();

        return $redisModel->delete($this->cacheKey.$id);
    }


    public function delAllCache()
    {
        $redisModel = new RedisModel();

        return $redisModel->deleteAll($this->cacheKey);
    }

}
